#define  _CRT_SECURE_NO_WARNINGS

#include "sort.h"

void swap(char* buf1, char* buf2, size_t width)
{
	int i = 0;
	for (i = 0; i < width; i++)
	{
		char tmp = *buf1;
		*buf1 = *buf2;
		*buf2 = tmp;
		buf1++;
		buf2++;
	}
}

void bubble_sort(void* base, size_t sz, size_t width, int (*cmp)(const void*, const void*))
{
	int i = 0;
	for (i = 0; i < sz - 1; i++)
	{
		int flag = 1;
		int j = 0;
		for (j = 0; j < sz - 1 - i; j++)
		{
			if (cmp((char*)base + j * width, (char*)base + (j + 1) * width) > 0)
			{
				flag = 0;
				swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
			}
		}
		if (1 == flag)
		{
			break;
		}
	}
}

void print_struct_name(struct people* p, size_t sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%s ", (p + i)->name);
	}
}

void print_struct_age(struct people* p, size_t sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d  ", (p + i)->age);
	}
}